define(function(require, exports, module) {
	
	require("layout");
	require("ztree");
	
	module.exports = function() {
		var zTree = null;
		var perms = {
			add:false,
			edit:false,
			remove:false
		}
		return {
			data: {
			},
			methods: {
				init: function(data) {
					var me = this;
					var params = {
						add:"sys:perm:manager:add",
						edit:"sys:perm:manager:edit",
						remove:"sys:perm:manager:remove"
					};
					//获取权限
					$.perms(params, function(isOk, data) {
						if(isOk) {
							$.extend(perms, data);
						}
						//初始化权限树
						me.initTree(function() {me.loading("close");});
					});
					return false;
				},
				//初始化权限树
				initTree:function(complete) {
					var me = this;
					var ul = $(this.$el).find("ul");
					var setting = {
						data: {
							simpleData: {
								enable: true,
								pIdKey: "parent_id"
							}
						},
						view: {
							addHoverDom:function(treeId, treeNode) {
								var container = $("#" + treeNode.tId + "_span");
								var buttons = container.children("span");
								if (treeNode.editNameFlag || buttons.length > 0) return;

								buttons = $("<span></span>").appendTo(container);
								if(perms.add) {
									var addButton = $('<span class="button add"></span>').appendTo(buttons);
									addButton.attr("title", $.i18n.prop("info.add"));
									addButton.css({
										"font-size":12
									});
									addButton.bind("click.treeItem", function() {
										me.showAdd(treeNode);
									});
								}
								if(treeNode.level == 0) {
									return;
								}
								if(perms.edit) {
									var editButton = $('<span class="button edit"></span>').appendTo(buttons);
									editButton.attr("title", $.i18n.prop("info.edit"));
									editButton.bind("click.treeItem", function() {
										me.showEdit(treeNode);
									});
								}
								if(treeNode.children && treeNode.children.length > 0) {
									return;
								}
								if(perms.remove) {
									var removeButton = $('<span class="button remove"></span>').appendTo(buttons);
									removeButton.attr("title", $.i18n.prop("info.remove"));
									removeButton.bind("click.treeItem", function() {
										me.confirmRemove(treeNode);
									});
								}
							},
							removeHoverDom:function(treeId, treeNode) {
								var container = $("#" + treeNode.tId + "_span");
								var buttons = container.children("span");
								buttons.find("a").unbind("click.treeItem");
								buttons.remove();
							}
						}
					};
					$.post("/api/permission/tree", {}, function(resp) {
						if(resp.success) {
							var rootNode = {
								id:"root",
								name:$.i18n.prop("sys.perm.manager"),
								open:true,
								children:resp.message
							};
							zTree = $.fn.zTree.init(ul, setting, rootNode);
							if(typeof(complete) == "function") {
								complete();
							}
						}
					}, "json");
				},
				showAdd: function(treeNode) {
					var extraParams = {
						node:treeNode,
						addNodes:function(parent, nodes) {
							zTree.addNodes(parent, nodes);
						}
					};
					$.window({
						router: "sys/perm/add",
						shade: 0.3
					}, extraParams);
				},
				showEdit: function(treeNode) {
					var extraParams = {
						node:treeNode,
						updateNode:function(node) {
							zTree.updateNode(node);
						}
					};
					$.window({
						router: "sys/perm/edit",
						shade: 0.3
					}, extraParams);
				},
				confirmRemove: function(treeNode) {
					$.confirm($.i18n.prop("sys.remove.confirm"), function(isOk) {
						if(isOk) {
							var _this = this;
							$.post("/api/permission/remove", {id:treeNode.id}, function(data) {
								if(data.success) {
									zTree.removeNode(treeNode);
								}
								_this.remove();
							}, "json");
						} else {
							this.remove();
						}
					}, true);
				},
				destroy: function() {
					zTree.destroy();
				}
			}
		}
	}
});